<?php

// 获取上级代理
if(!function_exists("Get_My_Parent_Agent"))
{
    function Get_My_Parent_Agent()
    {
        $login_user_id = session("user_id");
        $pid = M("agent")->where("agent_id = %d" ,array($login_user_id))->getField("parent_id");
        // 无上级代理
        if($pid == 0){
            return false;
        }
        $agent = M("agent a")
            ->join(C("DB_PREFIX")."agent_info ai ON a.agent_id = ai.agent_id" ,"LEFT")
            ->join(C("DB_PREFIX")."agent_rank ar ON a.rank_id = ar.rank_id" ,"LEFT")
            ->where("a.agent_id = $pid")
            ->field("a.*,ar.rank_name,ai.avatar")
            ->find();
        if($agent){
            return $agent;
        }else{
            return false;
        }
    }
}

if(!function_exists("img_upload"))
{
    function img_upload($file ,$dir ,$exts = array('jpg', 'png', 'jpeg') ,$fileSize = 3145728)
    {
        $uploadDir = "./Uploads/$dir/";
        if(!is_dir($uploadDir)) mkdir($uploadDir ,0777 ,true);
        $uploadDirs = "./Uploads/$dir/".date("Ym")."/";
        if(!is_dir($uploadDirs)) mkdir($uploadDirs ,0777 ,true);

        if($_FILES[$file]['name'] == ''){
            return array("error" => '找不到上传文件');
        }
        $file_ext = pathinfo($_FILES[$file]['name'] ,PATHINFO_EXTENSION);
        if(!in_array(strtolower($file_ext), $exts)){
            return array("error" => '上传文件类型不符合要求 '.$file_ext);
        }
        if($_FILES[$file]['size'] > $fileSize){
            return array("error" => '上传文件大小超过限制 '.$_FILES[$file]['size']);
        }
        $fileName = substr(md5(microtime() .$_FILES[$file]['name']) ,-15) .".". $file_ext;
        if(move_uploaded_file($_FILES[$file]['tmp_name'] ,$uploadDirs .$fileName)){
            return array($file => $fileName ,'path' => $uploadDirs);
        }else{
            return array("error" => '上传失败');
        }
    }
}

//多图片上传
if(!function_exists("img_uploads"))
{
    function img_uploads($file ,$dir ,$exts = array('jpg', 'png', 'jpeg') ,$fileSize = 3145728)
    {
        $uploadDir = "./Uploads/$dir/";
        if(!is_dir($uploadDir)) mkdir($uploadDir ,0777 ,true);
        $uploadDirs = "./Uploads/$dir/".date("Ym")."/";
        if(!is_dir($uploadDirs)) mkdir($uploadDirs ,0777 ,true);

        foreach($_FILES[$file]['name'] as $key =>$value ) {
            $_FILES[$file][$key]['name'] = $value;
            $_FILES[$file][$key]['type'] = $_FILES[$file]['type'][$key];
            $_FILES[$file][$key]['tmp_name'] = $_FILES[$file]['tmp_name'][$key];
            $_FILES[$file][$key]['error'] = $_FILES[$file]['error'][$key];
            $_FILES[$file][$key]['size'] =$_FILES[$file]['size'][$key];
        }
        unset($_FILES[$file]['name']);
        unset($_FILES[$file]['type']);
        unset($_FILES[$file]['tmp_name']);
        unset($_FILES[$file]['error']);
        unset($_FILES[$file]['size']);

        foreach ($_FILES[$file] as $gey =>$val){
            if($_FILES[$file][$key]['name'] == ''){
                return array("error" => '找不到上传文件');
            }
            $file_ext = pathinfo($_FILES[$file][$key]['name'] ,PATHINFO_EXTENSION);
            if(!in_array(strtolower($file_ext), $exts)){
                return array("error" => '上传文件类型不符合要求 '.$file_ext);
            }
            if($_FILES[$file][$key]['size'] > $fileSize){
                return array("error" => '上传文件大小超过限制 '.$_FILES[$file][$key]['size']);
            }
            $fileName = substr(md5(microtime() .$_FILES[$file][$key]['name']) ,-15) .".". $file_ext;
            if(move_uploaded_file($_FILES[$file][$key]['tmp_name'] ,$uploadDirs .$fileName)){
                $img_array[$key][$file] = $fileName;
                $img_array[$key]['path'] = $uploadDir;
//                return array($file => $fileName ,'path' => $uploadDirs);
            }else{
                return array("error" => '上传失败');
            }
        }
               return $img_array;
    };
}

// 获取 $agent_id 代理的等级 rank_grade
if(!function_exists("get_rank_grade"))
{
    function get_rank_grade( $agent_id)
    {
        return M("agent_rank")->where("rank_id = %d" ,array($agent_id))->getField("rank_grade");
    }
}

// 获取比 $agent_id 低等级的代理
if(!function_exists("get_ranks_low_agent"))
{
    function get_ranks_low_agent( $agent_id)
    {
        $rank_grade = get_rank_grade($agent_id);
        return M("agent_rank")->where("rank_status = 1 AND rank_grade >= $rank_grade")->select();
    }
}
//获取菜单名称
if(!function_exists("get_category"))
{
    function get_category(){
        $categories = M('category')->where(array('status'=>1))->order('id DESC')->select();
        $category = tree($categories);
        return $category;
    }
}
//获取月初时间戳
if(!function_exists("begin_This_month"))
{
    function begin_This_month()
    {
        $beginThismonth = mktime(0, 0, 0, date('m'), 1, date('Y'));
        return $beginThismonth;
    }
}
//获取月末时间戳
if(!function_exists("end_This_month"))
{
    function end_This_month()
    {
        $endThismonth = mktime(23, 59, 59, date('m'), date('t'), date('Y'));
        return $endThismonth;
    }
}
//获取昨天开始的时间戳
if(!function_exists("begin_Yesterday"))
{
    function begin_Yesterday()
    {
        $beginYesterday = mktime(0, 0, 0, date('m'), date('d') - 1, date('Y'));
        return $beginYesterday;
    }
}
//获取昨天结束的时间戳
if(!function_exists("end_Yesterday"))
{   function end_Yesterday()
    {
        $endYesterday = mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1;
        return $endYesterday;
    }
}
